package com.dhcc.bpm.modules.metadata.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.metadata.entity.VerifyRule;
import com.dhcc.bpm.modules.metadata.service.IVerifyRuleService;
import com.dhcc.bpm.modules.metadata.vo.VerifyRuleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * @ClassName VerifyRuleController
 * @Description 校验规则 控制器
 * @Author daina
 * @Date 2020/2/21 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-metadata/verifyrule")
@Api(value = "校验规则表", tags = "校验规则表接口")
public class VerifyRuleController extends BladeController {

	private IVerifyRuleService verifyRuleService;

	/**
	 * @Description 详情
	 * @Author daina
	 * @Date 10:49 2020/2/22
	 * @param verifyRule
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.metadata.entity.VerifyRule>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入verifyRule")
	@ApiLog("详情")
	@Log(title = "详情",businessType = BusinessType.SELECT)
	public R<VerifyRule> detail(VerifyRule verifyRule) {
		VerifyRule detail = verifyRuleService.getOne(Condition.getQueryWrapper(verifyRule));
		return R.data(detail);
	}

	/**
	 * @Description 分页 校验规则表
	 * @Author daina
	 * @Date 10:49 2020/2/22
	 * @param verifyRule
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.metadata.entity.VerifyRule>>
	 **/
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入verifyRule")
	@ApiLog("分页 校验规则表")
	@Log(title = "分页 校验规则表",businessType = BusinessType.SELECT)
	public R<IPage<VerifyRule>> list(@ApiIgnore @RequestParam Map<String, Object>  verifyRule, Query query) {
		IPage<VerifyRule> pages = verifyRuleService.page(Condition.getPage(query), Condition.getQueryWrapper(verifyRule, VerifyRule.class));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 校验规则表
	 * @Author daina
	 * @Date 10:50 2020/2/22
	 * @param verifyRule
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.metadata.vo.VerifyRuleVO>>
	 **/
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入verifyRule")
	@ApiLog("自定义分页 校验规则表")
	@Log(title = "自定义分页 校验规则表",businessType = BusinessType.SELECT)
	public R<IPage<VerifyRuleVO>> page(VerifyRuleVO verifyRule, Query query) {
		IPage<VerifyRuleVO> pages = verifyRuleService.selectVerifyRulePage(Condition.getPage(query), verifyRule);
		return R.data(pages);
	}

	/**
	 * @Description 新增 校验规则表
	 * @Author daina
	 * @Date 10:50 2020/2/22
	 * @param verifyRule
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入verifyRule")
	@ApiLog("新增校验规则表")
	@Log(title = "新增校验规则表",businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody VerifyRule verifyRule) {
		return R.status(verifyRuleService.save(verifyRule));
	}

	/**
	 * @Description 修改 校验规则表
	 * @Author daina
	 * @Date 10:50 2020/2/22
	 * @param verifyRule
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入verifyRule")
	@ApiLog("修改校验规则表")
	@Log(title = "修改校验规则表",businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody VerifyRule verifyRule) {
		return R.status(verifyRuleService.updateById(verifyRule));
	}

	/**
	 * @Description 新增或修改 校验规则表
	 * @Author daina
	 * @Date 10:50 2020/2/22
	 * @param verifyRule
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入verifyRule")
	@ApiLog("修改")
	@Log(title = "修改",businessType = BusinessType.UPDATE)
	public R submit(@Valid @RequestBody VerifyRule verifyRule) {
		verifyRule.setVerifyRuleName(verifyRule.getVerifyRuleName().trim());
		verifyRule.setVerifyRule(verifyRule.getVerifyRule().trim());
		try {
			return R.status(verifyRuleService.saveOrUpdate(verifyRule));
		} catch (Exception e) {
			if (e.getMessage().contains("for key 'rule_name_idx'") || e.getMessage().contains("ORA-00001")) {
				return R.fail(500, "该规则名称已存在");
			}
			return R.fail(500, "数据库异常");
		} finally {

		}
	}

	
	/**
	 * @Description 删除 校验规则表
	 * @Author daina
	 * @Date 10:50 2020/2/22
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@ApiLog("删除")
	@Log(title = "删除",businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(verifyRuleService.deleteLogic(Func.toLongList(ids)));
	}

	
}
